package corejava.io.mytest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;

public class ModifyFile {
	/**
	 * 缓冲区大小
	 */
	private static int BUFFER_SIZE = 3 * 1024 * 1024;
	/**
	 * 文件和路径名
	 */
	private static String filePath = "./logs/test.txt";
	
	/**
	 * 文件和路径名
	 */
	private static String destFilePath = "./logs/test02.txt";
	/**
	 * 要匹配的串
	 */
	private static String targetPattern = "Com";
	/**
	 * 替换后的串
	 */
	private static String replaceStr = "V10VXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
	/**
	 * 记录读写的行数
	 */
	private static long countLineNum = 0;
	
	/**
	 * 一行一行读取某个文件，然后做处理，之后写入一个新的文件中
	 * @throws IOException
	 */
	public static void readWriteFile() throws IOException {
		File file = new File(filePath);
		FileInputStream fin = new FileInputStream(file);
		// 将文件流组成缓冲字节输入流
		BufferedInputStream bis = new BufferedInputStream(fin);

		// 将缓冲字节输入流包装成字符输入流
		InputStreamReader isr = new InputStreamReader(bis,"GBK");// 不指定字符集，就使用默认字符集
		
		System.out.println(isr.getEncoding());
		System.out.println(System.getProperty("file.encoding"));
		BufferedReader br = new BufferedReader(isr, BUFFER_SIZE);

		File newfile = new File(destFilePath);
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
				new BufferedOutputStream(new FileOutputStream(newfile)),"GBK"));
		String str = null;
		while ((str = br.readLine()) != null) {
			countLineNum++;
			if (str.contains(targetPattern)) {
				str = str.replaceFirst(targetPattern,replaceStr);
				
			}
			bw.write(str+"\r\n");
			System.out.println("写入第"+countLineNum+"行: "+str);
		}
		bw.flush();
		bw.close();
		br.close();
		
		System.out.println("文件"+newfile+"总共有"+countLineNum+"行");

	}
	
	/**
	 * 随机访问文件，可读可写
	 * @param file
	 * @throws IOException
	 */
	public static void randomAccessFile(File file) throws IOException {
		RandomAccessFile  ra = new RandomAccessFile(file,"rw");
		
		//读一个字节
		//byte ch = ra.readByte();
		//System.out.println("Read first character of file: " + (char)ch);
		//读一行记录
		//String line = ra.readLine();
		//System.out.println("Read full line: " + line);
	 	
		//移动文件指针到指定的位置，此处移到到文件末尾
		//ra.seek(file.length());
	 	//换行ASCII值10，十六进制表示0x0A
	 	//ra.write(0x0A);
	 	//ra.writeBytes("This will complete the example.");
		long len = 0;
 		String line = null;
 		long num = 0;
 		//行结束符
 		String separator = System.getProperty("line.separator");
 		//行结束符的字节长度
 		int _len = separator.getBytes().length;
 		
		while ((line = ra.readLine()) != null) {
			num++;
			len = len + line.getBytes().length;
			System.out.println("读第"+num+"行:"+line);
			
			//保存当前文件指针
			long pos = ra.getFilePointer();
			if (line.contains(targetPattern)) {
				String temp = line.replaceFirst(targetPattern, replaceStr);
				//移动指针到当前行的航首
				ra.seek(pos - line.getBytes().length - _len);
				//修改当前行
				ra.writeBytes(temp+separator);
				pos = ra.getFilePointer();
				//替换的行
				System.out.println("第"+num+"行的内容被替换："+temp);

				//只修改找到的第一处匹配串，一旦找到，就跳出循环
				//break;
			}
		}
	 	
	 	ra.close();
	}
	
	/**
	 * 打印文件的前num行
	 * @param file
	 * @param num
	 * @throws IOException
	 */
	public static void readFile(File file, long num) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader(file),BUFFER_SIZE);
		long count = 0;
		String line = null;
 		System.out.println("==============================");
 		System.out.println("文件"+file+"内容：");
		while ((line = br.readLine()) != null) {
			count++;
			if (count <= num ) {
				System.out.println(line);
			}
		}
	}
	
	/**
	 * Oracle数据块的大小
	 */
	public static final int BLOCK_SIZE = 8192;
	/**
	 * 读取Oracle的exp工具导出的DMP文件
	 * 首行的第2个和第3个字节表示该文件的字符集编码
	 * @param file
	 */
	public static void readDmpFile(File file) throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
		
		byte[] chs = new byte[BLOCK_SIZE];
		//读首行前三个字节
		bis.read(chs, 0, BLOCK_SIZE);
		
		String str = new String(chs);
		System.out.println(str);
		

		for (byte b : chs) {
			//回车13，换行10
			System.out.println(b+"--"+(char)b);
		}
		
		
		bis.close();
		
		RandomAccessFile raf = new RandomAccessFile(file, "rw");
		
		byte[] bts = {3,88};
		//修改第2和第3个字节
		raf.seek(1);
		raf.write(bts);
		raf.close();
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		//readWriteFile();
		//randomAccessFile(new File(filePath));
		//readFile(new File(destFilePath), 10);
		//readDmpFile(new File("d:/cs.dmp"));
		
/*		RandomAccessFile raf = new RandomAccessFile(new File(filePath), "rw");
		String line = null;
		int i = 0;
		
		//系统行结束符：Windows：\r\n Linux/Unix: \n
		String separator = System.getProperty("line.separator");
		String tem = null;
		//如何替换呢？
		//tem = separator.replaceAll("\\", "//");
		System.out.println("系统换行符："+tem);
		while ((line = raf.readLine()) != null) {
			i++;
			System.out.println(i+": "+line);
		}*/
		
		//System.out.println(File.separator);
		
		//替换\r\n为\\r\\n
		String string = "\\r\\n";
		String temp = string.replaceAll("\\\\", "\\\\\\\\");
		System.out.println(temp);
	}

}
